﻿
var base_url = "http://localhost:8080/demo";

var fn_login = function () {

    var loginData = {
        "userName": $("#txtUserName").val(),
        "password": $("#txtPassword").val()
    };

    console.log(loginData);

    $.ajax({
        //请求方式
        type: "POST",
        //返回数据类型
        dataType: "json",
        //请求地址
        url: base_url + "/security/login",
        headers: {
            "x-requested-with": "XMLHttpRequest"
        },
        //数据，json字符串
        data: JSON.stringify(loginData),
        //跨域设置
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        //请求的媒体类型
        contentType: "application/json;charset=UTF-8",
        //请求成功
        success: function (result) {
            console.log(result);
            if (result.success) {
                console.log(result.data);
                localStorage.setItem("sessionId", result.data.sessionId);
                localStorage.setItem("jwt_token", result.data.token);
                alert("登陆成功");
                window.location = "index.html";
            }
            else {
                alert(result.message);
            }
        },
        //请求失败，包含具体的错误信息
        error: function (e) {
            console.log(e.status);
            console.log(e.responseText);
        }
    });

}

var fn_query_menu = function () {

    var requestData = {
        "name": $("#txtKeyword").val()
    };

    $("#txtResult").html("接口请求中...");
    console.log(requestData);

    var sessionId = localStorage.getItem("sessionId");
    console.log(sessionId);

    var token = localStorage.getItem("jwt_token");
    console.log(token);

    $.ajax({
        //请求方式
        type: "POST",
        //返回数据类型
        dataType: "json",
        //请求地址
        //url: base_url + "/base/menu" + ";jsessionid=" + sessionId, 方法一
        url: base_url + "/base/menu",
        //Token 写入Header
        //beforeSend: function (request) {
        //    request.setRequestHeader("jwt_token", "wxdong");
        //},
        //参数模式 写入Header
        headers: {
            "jwt_token": token,
            //"cors_jsessionid": sessionId, //Session 跨域
            "x-requested-with": "XMLHttpRequest"
        },
        //数据，json字符串
        data: JSON.stringify(requestData),
        //跨域设置
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        //请求的媒体类型
        contentType: "application/json;charset=UTF-8",
        //请求成功
        success: function (result) {
            console.log(result);
            if (result.success) {
                $("#txtResult").html(JSON.stringify(result.data));
            }
            else {
                alert(result.message);
            }
        },
        //请求失败，包含具体的错误信息
        error: function (e) {
            console.log(e.status);
            console.log(e.responseText);
            if (parseInt(e.status) === 401) {
                alert("请先登陆系统");
                window.location = "login.html";
            }
        }
    });

}

function fn_exit() {

    if (!confirm("确定要退出系统吗？？"))
        return;

    var sessionId = localStorage.getItem("sessionId");
    console.log(sessionId);

    var token = localStorage.getItem("jwt_token");
    console.log(token);

    $.ajax({
        //请求方式
        type: "POST",
        //返回数据类型
        dataType: "json",
        //请求地址
        //url: base_url + "/base/menu" + ";jsessionid=" + sessionId, 方法一
        url: base_url + "/security/logout",
        //Token 写入Header
        //beforeSend: function (request) {
        //    request.setRequestHeader("jwt_token", "wxdong");
        //},
        //参数模式 写入Header
        headers: {
            "jwt_token": token,
            //"cors_jsessionid": sessionId, //Session 跨域
            "x-requested-with": "XMLHttpRequest"
        },
        //数据，json字符串
        data: null,
        //跨域设置
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
        //请求的媒体类型
        contentType: "application/json;charset=UTF-8",
        //请求成功
        success: function (result) {
            console.log(result);
            if (result.success) {
                //window.location = "login.html";
            }
            else {
                alert(result.message);
            }
        },
        //请求失败，包含具体的错误信息
        error: function (e) {
            console.log(e.status);
            console.log(e.responseText);
            if (parseInt(e.status) === 401) {
                alert("请先登陆系统");
                window.location = "login.html";
            }
        }
    });

}

function fn_upload_show() {
    dialog.dialog("#dialog-upload", "上传图片", '', function () {

        var token = localStorage.getItem("jwt_token");
        console.log(token);

        var formData = new FormData($('#uploadForm')[0]);

        $.ajax({
            type: 'post',
            url: base_url + "/base/media/upload",
            headers: {
                "jwt_token": token,
                //"cors_jsessionid": sessionId, //Session 跨域
                "x-requested-with": "XMLHttpRequest"
            },
            data: formData,
            cache: false,
            processData: false,
            contentType: false
        }).success(function (result) {
            console.log(result);
            if (result.success) {                
                $("#txtResult").html("<div>上传图片成功：" + result.data.materialName+"</div><div><img src='" + result.data.resourceUrl + "' title='" + result.data.materialName+"' /></div>");
            }
            else {
                alert(result.message);
            }
        }).error(function (e) {
            console.log(e.status);
            console.log(e.responseText);
            if (parseInt(e.status) === 401) {
                alert("请先登陆系统");
                window.location = "login.html";
            }
        });

    }, function () {
        alert("你点了取消");
    }, true, 400);
}

